//
//  FIFaceView.m
//  cherryproduct
//
//  Created by zou shigang on 14/9/8.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "FIFaceView.h"

@interface FIFaceView ()

@property (nonatomic, strong) NSMutableArray * array_m_face;
@property (nonatomic, strong) NSString * str_face_context;

@end

@implementation FIFaceView

- (id)initWithFrame:(CGRect)frame withContent:(NSString *)faceContent
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        if (self.array_m_face == nil) {
            self.array_m_face = [[NSMutableArray alloc] initWithCapacity:18];
            
            for (int i = 0; i < 18; i++) {
                NSString * str_face = [NSString stringWithFormat:@"face%d", i];
                [self.array_m_face addObject:[UIImage imageNamed:str_face]];
            }
        }
        self.str_face_context = faceContent;
        
        self.backgroundColor=[UIColor whiteColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
	// Color and font for the main text items (time zone name, time)
	UIColor * mainTextColor = nil;
	UIFont * mainFont = [UIFont boldSystemFontOfSize:10];

    mainTextColor = [UIColor blackColor];
    [mainTextColor set];
    
    CGPoint point = CGPointZero;
    CGRect contentRect = self.bounds;
    
    NSArray * array_info = [self.str_face_context componentsSeparatedByString:@"[qqface]"];
    for (int i = 0; i < [array_info count]; i++) {
        NSString * item = [array_info objectAtIndex:i];
        
        NSRange r = [item rangeOfString:@"[/qqface]" options:NSCaseInsensitiveSearch];
        if (r.length > 0) {
            NSInteger n_num = [item substringToIndex:r.location].intValue;
            if (n_num >= 0 && n_num < 18) {
                [[self.array_m_face objectAtIndex:n_num] drawInRect:CGRectMake(point.x, point.y, 20, 20)];
                point.x += 20+5;
                if (point.x > contentRect.size.width) {
                    point.x = 0;
                    point.y = point.y+21;
                }
            }
            
            //后面是否有字
            int bbd = r.length+r.location;
            if (item.length > bbd) {
                NSString * str_dd = [item substringFromIndex:bbd];
                
                CGSize si = [str_dd sizeWithFont:mainFont];
                
                [str_dd drawAtPoint:point forWidth:si.width withFont:mainFont minFontSize:10 actualFontSize:NULL lineBreakMode:NSLineBreakByWordWrapping baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
                
                point.x += si.width;
                if (point.x > contentRect.size.width) {
                    point.x = 0;
                    point.y = point.y+21;
                }
            }
        } else {
            
            CGSize si = [item sizeWithFont:mainFont];
            
            [item drawAtPoint:point forWidth:si.width withFont:mainFont minFontSize:10 actualFontSize:NULL lineBreakMode:NSLineBreakByWordWrapping baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
            
            point.x += si.width;
            if (point.x > contentRect.size.width) {
                point.x = 0;
                point.y = point.y+21;
            }
        }
    }
}

- (NSArray *)rangesOfString:(NSString *)searchString inString:(NSString *)str
{
    NSMutableArray *results = [NSMutableArray array];
    NSRange searchRange = NSMakeRange(0, [str length]);
    NSRange range;
    while((range = [searchString rangeOfString:str options:0 range:searchRange]).location != NSNotFound)
    {
        [results addObject:[NSValue valueWithRange:range]];
        searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
    }
    return results;
}

@end























